import { config } from '@vue/test-utils'
import { vi } from 'vitest'
import { server } from './mock/server';
import { App } from '@/app/app';
import { Http } from '@/http';
import { AxiosResponse } from 'axios';
import { HttpResponse} from '@/core';
import { TestContext } from 'vitest';
import { i18n, setupI18N } from '@/locales';
import { Button, InputNumber, Input, InputPassword, Textarea, Select, TreeSelect, Cascader } from 'ant-design-vue';

// 设置全局变量
// 设置 App
const app = App.getInstance();
vi.stubGlobal('App', app);
// 设置 i18n
const VueApp = createApp(App);
await setupI18N(VueApp);
vi.stubGlobal('i18n', i18n);
// 设置事件监听
const emit = vi.fn((event: string, name: string, action: any, data: any) => ({}));
vi.stubGlobal('emit', emit);

// 设置测试参数
beforeAll(() => server.listen({ onUnhandledRequest: 'error' }));
afterAll(() => server.close());
beforeEach(async (context: TestContext) => {
    // 设置测试上下文
	// 拦截器
	Http.getHttp().interceptors.response.use((response: AxiosResponse) => {
		return new HttpResponse(response.data, response);
	});
});
afterEach(() => server.resetHandlers());

// ant-design组件存根
config.global.stubs = {
	['a-button']: Button,
	['a-input-number']: InputNumber,
	['a-input-password']: InputPassword,
	['a-input']: Input,
	['a-textarea']: Textarea,
	['a-cascader']: Cascader,
	['a-tree-select']: TreeSelect,
	['a-select']: Select
}
